package com.jweight;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class JFileWriter {
	HashMap<String, Double> agestWeigthValMap=new HashMap<String, Double>();;
	ArrayList<String> historyValues=null;
	BufferedReader textFileReader=null;
	PrintWriter textFileWriter=null;
	String tString=null;
	File writeToFile=null;
	public JFileWriter() {
		
		// TODO Auto-generated constructor stub
	}

	public void writeToFile(String context,String FilePath){
		ArrayList<String> historyValues=new ArrayList<String>();
		try{
			writeToFile=new File(FilePath);	
			if(writeToFile.exists()){
				
				textFileReader=new BufferedReader(new FileReader(writeToFile));
				 while ((tString=textFileReader.readLine()) != null) {
					 String refinedStr=tString.trim();
					 if(refinedStr.equals("seperator")){
						 
						 String val=""+agestWeigthValMap.get(context);
						 System.out.println(val);
						 historyValues.add(val);
						 historyValues.add("seperator");
						 continue;
						}
					 historyValues.add(refinedStr);
				 }
			}else{
				writeToFile.createNewFile();				
			}			
			 if(historyValues.size()==0){
				 String val=""+agestWeigthValMap.get(context);				
				 historyValues.add(val);
				 historyValues.add("seperator");
			 }		
			 textFileWriter=new PrintWriter(new FileWriter(writeToFile));
			 for(int j=0;j<historyValues.size();j++){
				 String myString=historyValues.get(j).toString();
				 System.out.println(myString);
				 textFileWriter.println(myString);
			 }
			 textFileWriter.flush();
			 textFileWriter.close();
			 if(textFileReader!=null){
				 textFileReader.close();
			 }
			
			}catch (Exception e) {
				// TODO: handle exception
				System.out.println(e.getMessage());
			}
			
	}
	public void writeToFile(Double confidence,String FilePath){
		ArrayList<String> historyValues=new ArrayList<String>();
		try{
			writeToFile=new File(FilePath);	
			if(writeToFile.exists()){
				
				textFileReader=new BufferedReader(new FileReader(writeToFile));
				 while ((tString=textFileReader.readLine()) != null) {
					 String refinedStr=tString.trim();
					 if(refinedStr.equals("seperator")){					 
						 String val=""+confidence;
						 historyValues.add(val);
						 historyValues.add("seperator");
						 continue;
						}
					 historyValues.add(refinedStr);
				 }
			}else{
				writeToFile.createNewFile();				
			}
			
			 if(historyValues.size()==0){
				 String val=""+confidence;
				 historyValues.add(val);
				 historyValues.add("seperator");
			 }
			// historyValues.add(Value);
			 textFileWriter=new PrintWriter(new FileWriter(writeToFile));
			 for(int j=0;j<historyValues.size();j++){
				 String myString=historyValues.get(j).toString();
				 System.out.println(myString);
				 textFileWriter.println(myString);
			 }
			 textFileWriter.flush();
			 textFileWriter.close();
			 if(textFileReader!=null){
				 textFileReader.close();
			 }
			
			}catch (Exception e) {
				// TODO: handle exception
				System.out.println(e.getMessage());
			}
	}


	public void createAllFiles(HashMap<String, Double> agestVal,Double confidence){
		
		agestWeigthValMap=agestVal;
		Iterator<String> iter = agestWeigthValMap.keySet().iterator();
		
		while (iter.hasNext()) {
            String tempConVar = iter.next();
            String s="C:\\Documents and Settings\\PI05\\Desktop\\file\\"+tempConVar+".text";
			writeToFile(tempConVar,s);
          }
	
		writeToFile( confidence,"C:\\Documents and Settings\\picSEEK\\Desktop\\XMLRepository\\Confidence.txt");
	}
	
}

